<?php
namespace app\index\controller;
use app\model\Product as ProductModel;
use think\Db;


class Product extends Base
{
    public $exception = ['index'];

    public function __construct(){
        $model = new ProductModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '产品';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function detail($id){
        $model = $this->model;
        $params = [
            'price_type'=>input('price_type','hour'),
            'gpu_num'=>input('gpu_num',1)
        ];

        $price_types = $model->getPriceTypeList();

        $data = ProductModel::where('id',$id)->find();
        if (!empty($data)) {
            $data['region_name'] = Db::name('region')->where('id',$data['region_id'])->value('name');

            switch ($params['price_type']) {
                case 'hour': 
                    $data['price'] = $data['hour_price'];
                    $data['unit'] = '时';
                    break;
                case 'days': 
                    $data['price'] = $data['days_price'];
                    $data['unit'] = '日';
                    break;
                case 'week': 
                    $data['price'] = $data['week_price'];
                    $data['unit'] = '周';
                    break;
                case 'month': 
                    $data['price'] = $data['month_price'];
                    $data['unit'] = '月';
                    break;
                default: break;
            }
        }

        
        return view('detail', [
            'data'=>$data,
            'price_types'=>$price_types,
            'params'=>$params
        ]);
    }

    public function buy(){
        $fields = [
            'must'=>['id','price_type','gpu_num'],
        ];
        $params = paramFilter(request()->param(),$fields);
        if (!empty($params['error_msg'])) {
            $this->error($params['error_msg']);
        }

        $data = ProductModel::where('id',$params['id'])->find();
        switch ($params['price_type']) {
            case 'hour': 
                $data['price'] = $data['hour_price'];
                $data['unit'] = '时';
                break;
            case 'days': 
                $data['price'] = $data['days_price'];
                $data['unit'] = '日';
                break;
            case 'week': 
                $data['price'] = $data['week_price'];
                $data['unit'] = '周';
                break;
            case 'month': 
                $data['price'] = $data['month_price'];
                $data['unit'] = '月';
                break;
            default: break;
        }

        $save_data = [
            'user_id'=>$this->login_user['id'],
            'order_sn'=>uuidCreate(),
            'product_id'=>$params['id'],
            'price_type'=>$params['price_type'],
            'gpu_num'=>$params['gpu_num'],
            'ctime'=>time(),
            'price'=>$data['price'],
            'total_price'=>$data['price']*$params['gpu_num']
        ];

        if (Db::name('order')->insert($save_data)) {
            $this->success('下单成功');
        }else{
            $this->error('下单失败，请重试');
        }
    }

    public function index(){
        $model = $this->model;

        $regions = Db::name('region')->select();
        $regions = array_chunk($regions, 7);
        $price_types = [
            'hour'=>'按量计费',
            'days'=>'包日',
            'week'=>'包周',
            'month'=>'包月'
        ];

        
        $params = [
            'price_type'=>input('price_type','hour'),
            'region_id'=>input('region_id',$regions[0][0]['id']),
            'gpu_model'=>input('gpu_model'),
            'gpu_num'=>input('gpu_num',1)
        ];
        // dump(in_array('RTX 4090',$params['gpu_model']));
        // dump($params);die;

        $map = [];
        switch ($params['price_type']) {
            case 'hour': $map[] = ['hour_price','>',0]; break;
            case 'days': $map[] = ['days_price','>',0]; break;
            case 'week': $map[] = ['week_price','>',0]; break;
            case 'month': $map[] = ['month_price','>',0]; break;
            default: break;
        }
        $map[] = ['region_id','=',$params['region_id']];
        $map[] = ['total_num','>=',$params['gpu_num']];
        
        // $gpu_models = Db::name('gpu_model')->select();
        $gpu_models = Db::name('product')->where($map)->column('gpu_model');
        $gpu_models = array_unique($gpu_models);

        if (!empty($params['gpu_model'])) {
            $map[] = ['gpu_model','in',$params['gpu_model']];
        }


        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',10)
        ];
        $list = $model->where($map)->order('valid_num desc,ctime desc')->paginate($page_param);
        // $page = $list->render();
        // $page = indexPage($page);

        $list = $list->toarray();


        $page_param = $params;
        $page = [
            'total'=>$list['total'],
            'last'=>$list['last_page'],
            'current'=>$list['current_page'],
            'nums'=>[]
        ];
        if ($page['current'] != 1) {
            $page_param['page'] = $page['current'] -1;
            $page['prev'] = [
                'num'=>$page['current'] -1,
                'url'=>url('index',$page_param)
            ];
        }
        if ($page['current'] != $page['last']) {
            $page_param['page'] = $page['current'] +1;
            $page['next'] = [
                'num'=>$page['current'] +1,
                'url'=>url('index',$page_param)
            ];
        }
        for($i = 1;$i <= $page['last'];$i++){
            $page_param['page'] = $i;
            $page['nums'][] = [
                'num'=>$i,
                'url'=>url('index',$page_param)
            ];
        }

        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['region_name'] = Db::name('region')->where('id',$vo['region_id'])->value('name');

            switch ($params['price_type']) {
                case 'hour': 
                    $list['data'][$k]['price'] = $vo['hour_price'];
                    $list['data'][$k]['unit'] = '时';
                    break;
                case 'days': 
                    $list['data'][$k]['price'] = $vo['days_price'];
                    $list['data'][$k]['unit'] = '日';
                    break;
                case 'week': 
                    $list['data'][$k]['price'] = $vo['week_price'];
                    $list['data'][$k]['unit'] = '周';
                    break;
                case 'month': 
                    $list['data'][$k]['price'] = $vo['month_price'];
                    $list['data'][$k]['unit'] = '月';
                    break;
                default: break;
            }
        }




        
        return view('index', [
            'list'=>$list,
            'page'=>$page,

            'params'=>$params,

            'price_types'=>$price_types,
            'regions'=>$regions,
            'gpu_models'=>$gpu_models
        ]);
    }


    
}
